﻿//https://leetcode.cn/problems/number-of-islands/?envType=study-plan-v2&envId=top-100-liked

class Solution {
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    bool vist[301][301];
    int ret, n, m;
public:
    int numIslands(vector<vector<char>>& grid)
    {
        n = grid.size(), m = grid[0].size();
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (!vist[i][j] && grid[i][j] == '1')
                {
                    ret++;
                    dfs(grid, i, j);
                }
            }
        }
        return ret;
    }

    void dfs(vector<vector<char>>& grid, int i, int j)
    {
        vist[i][j] = true;

        for (int k = 0; k < 4; k++)
        {
            int a = i + dx[k];
            int b = j + dy[k];
            if (a >= 0 && a < n && b >= 0 && b < m && !vist[a][b] && grid[a][b] == '1')
            {
                dfs(grid, a, b);
            }
        }
    }
};